package com.example.demo.practice;

import java.util.ArrayList;
import java.util.List;

public class Test_全排列 {

    public static List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        boolean [] usedNums = new boolean[nums.length];
        process(0,res,new ArrayList<>(),usedNums, nums);
        return res;
    }


    public static void process(int n, List<List<Integer>> res, List<Integer> list, boolean [] usedNums, int [] nums){

        if( n > nums.length){
            return;
        }
        if( n == nums.length){
            res.add(new ArrayList<>(list));
            return;
        }

        for(int i =0; i<nums.length;i++){
            if(usedNums[i]){
                continue;
            }
            list.add(nums[i]);
            usedNums[i]=true;
            process(n+1,res,list,usedNums,nums);
            list.remove(list.size()-1);
            usedNums[i]=false;
        }
    }


    public static void main(String[] args) {
        int [] nums = {1,0,-1};
        List<List<Integer>> permute = permute(nums);
        permute.stream().forEach(x->{
            x.stream().forEach(y-> System.out.print(y+" "));
            System.out.println();
        });
    }

}
